<?php

/**
 * 消息发布者
 * http方式
 */

namespace TigrisMQ;

class HttpProducer
{
    private $topic;
    private $url;
    private $accessKey;
    private $app_id;

    public function __construct($config)
    {
        $this->topic = isset($config['Topic']) ? $config['Topic'] : '';
        $this->url = isset($config['URL']) ? $config['URL'] : '';
        $this->accessKey = isset($config['AccessKey']) ? $config['AccessKey'] : '';
        $this->app_id = isset($config['app_id']) ? $config['app_id'] : '';
    }

    /**
     * 写入消息
     * @param array $data 写入的数据
     * @return array
     */
    public function sendMessage($data = [])
    {
        $parameters = [
            'topic' => $this->topic,
            'object' => json_encode($data)
        ];
        return Util::doRequest($this->url, 'POST', '/message/post/', $this->app_id, $this->accessKey, $parameters);
    }

}
